
// import { queryUserMenuTree } from '../api/base/role/ha_role_user';
export async function generateMenus() {
  // 动态路由写法
  // let menuList: API.Menu[] = [];
  // let templist: API.MenuItemList = [];
  // // todo 动态路由接口
  // const res = await queryUserMenuTree();
  // templist = res.data;

  // const hideMenu: string[] = []; // 隐藏的菜单
  // clapMenu(templist, menuList, hideMenu);
  // const temp2list: API.Menu[] = [];
  // menuList.forEach((v) => {
  //   if (v.type === 0 || v.type === 1) {
  //     temp2list.push(v);
  //   }
  // });
  // menuList.forEach((v) => {
  //   if (v.type === 3) {
  //     temp2list.push(v);
  //   }
  // });

  // menuList = temp2list;

  let menuList: API.Menu[] = []
  let list = []
  let hideMenu: string[] = []; // 隐藏的菜单
  clapMenu(list, menuList, hideMenu)
  let menuList2: API.Menu[] = require("../router/asyncMenus3.json")
  menuList = menuList2

  return { menuList, hideMenu };
}

/**
 * 把父级菜单转成层级关系的列表, 框架会自己组装
 * @param menuTree 后端接口查询到的菜单树
 * @param menuList 框架需要的菜单列表
 */
function clapMenu(menuTree: API.MenuItemList, menuList: API.Menu[], hideMenu: string[]): void {
  menuTree.map((v) => {
    // 顶级菜单项
    if (v.pid == '0') {
      // 将后端菜单格式转化为框架菜单格式
      menuList.push(repEnum(v));
    } else if (v.routetype == 0) {
      // 子集菜单
      const menuItem: API.Menu = repEnum(v);
      // 找子级菜单下的重定向页面
      for (const s of v.children) {
        if (v.redirect == s.path) {
          // 把list页面的路由地址, 关联到显示菜单下
          menuItem.viewPath = `${s.component}.vue`;
          menuItem.type = 1;
          menuItem.router = s.path;
          menuList.push(menuItem);
        } else {
          const HmenuItem: API.Menu = repEnum(s);
          HmenuItem.type = 3;
          menuList.push(HmenuItem);
          hideMenu.push(HmenuItem.router);
        }
        if (s.children && s.children.length > 0) {
          clapMenu(s.children, menuList, hideMenu);
        }
      }
    }

    if (v.children && v.children.length > 0) {
      clapMenu(v.children, menuList, hideMenu);
    }
  });
}

/**
 * 将后端菜单格式转化为框架菜单格式
 * @param menu
 * @returns
 */
function repEnum(menu: API.MenuItem): API.Menu {
  // 框架需要的菜单子项
  const menuItem: API.Menu = {
    id: menu.id,
    parentId: menu.pid == '0' ? null : menu.pid,
    name: menu.description,
    router: menu.name,
    perms: null,
    type: menu.routetype,
    icon: menu.icon,
    orderNum: menu.seqnum,
    viewPath: menu.component ? `${menu.component}.vue` : menu.component,
    keepalive: true,
    isShow: menu.routetype == 0 ? true : false,
    isExt: false,
    openMode: 1,
  };
  return menuItem;
}

/**
 * 删除隐藏菜单
 * @param menuTree 渲染菜单树
 * @param hideMenu 隐藏菜单id
 */
export function filterHideMenu(menuTree: Array<any>, hideMenu: string[]): Array<any> {
  menuTree.map((v) => {
    if (v.children) {
      for (let i = 0; i <= v.children.length - 1; i++) {
        if (hideMenu.includes(v.children[i].path)) {
          v.children.splice(i);
        }
      }
      // 三级菜单这边后续处理, 可能存在显示问题
      if (v.children && v.children.length > 0) {
        filterHideMenu(v.children, hideMenu);
      }
    }
  });

  return menuTree;
}